<?php 
class accountsAction extends AdminMaster{
	private $tbl = null;
	public function execute($request){
		sfContext::getInstance()->getConfiguration()->loadHelpers('I18N');
		$this->tbl = new SysAccounts();
		$this->data['navigation']	= __('module_sys_accounts',null,'menu').' › '. __('module_sys_accounts_action_accounts',null,'menu');
		
		
		$this->formData				= $request->getParameter('SysAccounts');	//get form data from add new || edit page
		
		
		switch ($this->data['action']){
			case ADMIN_DEFINED::DELETE_RECORD:
				$this->delRecord($this->data['select_list']);
				break;
				
			case ADMIN_DEFINED::DISABLE:
				$this->disableRecord($this->data['select_list']);
				break;
				
			case ADMIN_DEFINED::ENABLE:
				$this->enableRecord($this->data['select_list']);
				break;
			
			/*
			 * ADDING RECORD
			 */
			case ADMIN_DEFINED::ADD_RECORD:
				$this->data['is_add'] = 1;
				$this->addRecord();
				break;
			/*
			 * EDIT RECORD
			 */
			case ADMIN_DEFINED::EDIT_RECORD:
				$this->data['is_edit'] = 1;
				$this->editRecord();
				break;
				
			/*
			 * ADD MORE ACTION IN HERE
			 */
				
			/*
			 * DEFAULT: SHOW DATA AS TABLE LIST 
			 */	
			default: 
				$this->filterList($this->data['filter']);
				break;
		}
		
		$this->setVar('data', $this->data);
	}	
	
	/*
	 * SET ACTION EACH DATA ROW
	 */
	private function setAction(){
		$action = array();
		$action[]=array(
				'href'	=>	'?action_name='.ADMIN_DEFINED::EDIT_RECORD.'&id=%s',
				'text'	=>	__('action_edit',null,'table_list'),
				'title' =>	__('action_edit',null,'table_list'),
		);
		
		$this->data['action_list']	= $action;
	}
	
	/*
	 * SHOW DATA AS TABLE LIST CODE
	 */
	private function filterList($data = array()){
		$this->setAction();
		$this->data['navigation'].=' › '. __('nav_list',null,'table_list');
		
		$this->data['list'] = $this->tbl->filterList($data);
	}
	
	/*
	 * DELETE
	 */
	private function delRecord($data = array()){
		//checking user
		if(!is_array($data)){
			$data = array($data);
		}
		$this->tbl->deleteData($data);
		$this->filterList($this->data['filter']);
	}
	
	/*
	 * DISABLE
	 */
	private function disableRecord($data = array()){
		//checking user
		if(!is_array($data)){
			$data = array($data);
		}
		$this->tbl->disableData($data);
		$this->filterList($this->data['filter']);
	}
	
	/*
	 * ENABLE
	 */
	private function enableRecord($data = array()){
		//checking user
		if(!is_array($data)){
			$data = array($data);
		}
		$this->tbl->enableData($data);
		$this->filterList($this->data['filter']);
	}
	
	/*********************************************************************************
	*
	* ADD NEW
	* 
	**********************************************************************************/
	private function addRecord(){
		$this->data['navigation'].=' › '. __('nav_add_new',null,'table_list');
		$this->data['button_action'] = __('button_add',null,'table_list');
		$this->setTemplate('accountsForm');
		
		$form = new SysAccountsForm();
		if($this->data['is_post']){
			$form->bind($this->formData);
			if($form->isValid()){
				if($this->tbl->count($this->formData)>0){
					$this->data['result']	= sprintf(__('db_existing', null, 'sys_account'), sfConfig::get('app_email_admin')) ;
				}elseif($this->tbl->addData($this->formData)){
					$this->data['result']	= __('add_new_success', null, 'sys_account');
					$form = new SysAccountsForm();
				}else{
					$this->data['result']	= sprintf(__('add_new_error', null, 'table_list'), sfConfig::get('app_email_admin')) ;
				}
			}
			/* for debug
			else{
				foreach($form->getFormFieldSchema() as $name=>$formField){
					echo $formField->getName();
					echo $formField->getError(); 
				}
			}*/
		}
		$this->setVar('form', $form);
	}
	
	/*
	 * EDIT
	 */
	private function editRecord(){
		$this->data['navigation'].=' › '. __('nav_edit',null,'table_list');
		$this->data['action'].='&id='.$this->data['id'];
		$this->data['button_action'] = __('button_edit',null,'table_list');
		$this->setTemplate('accountsForm');
		
		$form = new SysAccountsForm();
		if($this->data['is_post']){
			$form->bind($this->formData);
			if($form->isValid()){
				$this->formData['id'] = $this->data['id'];
				if($this->tbl->count($this->formData)>1){
					$this->data['result']	= sprintf(__('db_existing', null, 'sys_account'), sfConfig::get('app_email_admin')) ;
				}elseif($this->tbl->updateData($this->formData)){
					$this->data['result']	= __('edit_success', null, 'sys_account');
				}else{
					$this->data['result']	= sprintf(__('edit_error', null, 'table_list'), sfConfig::get('app_email_admin')) ;
				}
				
			}
		}else{
			$data = $this->tbl->getById($this->data['id']);
			if(count($data)<0){
				$this->redirect($this->data['redirect']);
			}else{
				$this->formData = array(
							'name'		=> $data['name'],
							'email'		=> $data['email'],
							'login_user'=> $data['login_user'],
							'group_id'	=> $data['group_id'],
							'is_enabled'=> $data['is_enabled'],
							'_csrf_token'=> $form->getCSRFToken()
						);
				$form->bind($this->formData);
			}
		}
		$this->setVar('form', $form);
		
	}
}